package com.jwaoo.mobile.view.forgotpassword;

import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import com.dx.as.JWCache;
import com.dx.as.LoadingDialog;
import com.dx.as.ToolBarOptions;
import com.dx.as.UI;
import com.dx.as.common.JWConstant;
import com.dx.as.utils.AESSecurityUtil;
import com.jwaoo.mobile.R;
import com.jwaoo.mobile.view.forgotpassword.presenter.PasswordPresenter;
import com.jwaoo.mobile.view.main.MainActivity;

/**
 * Created by jwaoo on 2017/10/18.
 */

public class UpdatePasswordActivity extends UI implements IPasswordView{
    private EditText edt_password;
    private EditText edt_confirm_password;
    private CheckBox cb_show_password;
    private boolean checked = false;
    public final static String password ="orgPassword";
    public final static String isFormLogin = "from_login";
    private PasswordPresenter presenter;
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login_modify_password);
        initToolbar();
        initView();
        presenter = new PasswordPresenter(this,this);
    }

    private void initView(){
        edt_password = findView(R.id.edt_password);
        edt_confirm_password = findView(R.id.edt_confirm_password);
        cb_show_password = findView(R.id.cb_show_password);
    }

    private void initToolbar(){
        ToolBarOptions toolBarOptions = new ToolBarOptions();
        toolBarOptions.showLeft = true;
        toolBarOptions.showRight = true;
        toolBarOptions.titleString = getResources().getString(R.string.modify_password);
        toolBarOptions.rightOptionString = getResources().getString(R.string.Save);
        toolBarOptions.onRightClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPassword = edt_password.getText().toString();
                String confirmPassword = edt_confirm_password.getText().toString();
                try{
                    if (newPassword.equals(confirmPassword)) {
                        setRightBtnEnable(false);
                        LoadingDialog.getInstance(UpdatePasswordActivity.this).show();
                        presenter.doUpdatePassword(getIntent().getStringExtra(password),new String(AESSecurityUtil.encrypt(newPassword, JWConstant.KEY_AES)));
                    }else {
                        showSnackBar("密码不一致!");
                    }
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        };
        toolBarOptions.onLeftClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goWhere();
            }
        };
        setToolbar(toolBarOptions);
    }

    public void onViewClick(View view){
        checked = !checked;
        cb_show_password.setChecked(checked);
        edt_password.setTransformationMethod(checked ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
        edt_confirm_password.setTransformationMethod(checked ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
    }

    @Override
    public void onNotPerfect() {
        LoadingDialog.getInstance(JWCache.getContext()).dismiss();
        goWhere();
    }

    private void goWhere(){
        if (getIntent().getBooleanExtra(isFormLogin,false)){
            goToNewActivity(MainActivity.class);
        }
        finish();
    }

    @Override
    public void onHttpRequestComplete(boolean isSuccess) {
        if (isSuccess) finish();
        setRightBtnEnable(true);
    }
}
